/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tchepannou.payment.api;

import java.io.IOException;

/**
 * This interface represent a service that handle payments.
 * 
 * @author herve
 */
public class PaymentService
{
    //-- Attributes
    private PaymentGateway _gateway;
    private PaymentListener _listener;

    //-- Constructor
    public PaymentService (PaymentGateway gateway)
    {
        _gateway = gateway;
    }


    //-- PaymentService overrides
    public PaymentResponse process (PaymentRequest request)
        throws IOException
    {
        PaymentResponse response = _gateway.process (request);
        notifyListener (request, response);
        return response;
    }

    public void setPaymentListener (PaymentListener listener)
    {
        _listener = listener;
    }


    //-- Private method
    private void notifyListener (PaymentRequest request, PaymentResponse response)
    {
        if (_listener == null)
        {
            return;
        }
        _listener.onProcess (request, response);
    }
}